/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JOptionPane;
import src.Core;

/**
 *
 * @author Strauzen
 */
public class VentanaConsultaSimple extends javax.swing.JFrame {

    Ventana1 v1;
    
    /**
     * Creates new form VentanaConsultaSimple
     */
    public VentanaConsultaSimple(Ventana1 v1) {
        this.v1 = v1;
        initComponents();
    }

    private LinkedList<String> parsearTexto(){
        String s = this.jTextField_vcsPalabras.getText();
        String aux ="";
        LinkedList<String> result = new LinkedList<>();
        for(int i = 0; i < s.length(); i++)
        {
            if(s.charAt(i) != ','){
                aux +=s.charAt(i);
            }else {
                result.add(aux);
                aux ="";
            }
        }
        result.add(aux);
        return result;
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel_vcsPalabras = new javax.swing.JLabel();
        jTextField_vcsPalabras = new javax.swing.JTextField();
        jButton_vcsAceptar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        jLabel_vcsPalabras.setText("Ponga aqui separadas por comas las palabras que desee consultar:");

        jButton_vcsAceptar.setText("Aceptar");
        jButton_vcsAceptar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton_vcsAceptarMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel_vcsPalabras)
                .addGap(0, 67, Short.MAX_VALUE))
            .addComponent(jTextField_vcsPalabras, javax.swing.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 318, Short.MAX_VALUE)
                .addComponent(jButton_vcsAceptar))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel_vcsPalabras)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField_vcsPalabras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton_vcsAceptar))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_vcsAceptarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton_vcsAceptarMouseClicked
        // TODO add your handling code here:
        LinkedList<String> contenido = parsearTexto();
        Iterator i = contenido.iterator();
        if(i.hasNext()){
            String text = Core.buscaEnLaBD(contenido);
            VentanaConsultaEleccion vce = new VentanaConsultaEleccion(v1);
            vce.modificarTexto(text);
            this.setVisible(false);
            vce.setVisible(true);
            this.dispose();
        }
        else
            JOptionPane.showMessageDialog(null, "Introduzca las palabras que desee buscar separadas por ','\nej. Pedro,Madrid\n\n");
    }//GEN-LAST:event_jButton_vcsAceptarMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_vcsAceptar;
    private javax.swing.JLabel jLabel_vcsPalabras;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField_vcsPalabras;
    // End of variables declaration//GEN-END:variables
}
